// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.textures;

import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;

public final class TexturePackPage {
    public static HashMap<String, Stack<String>> FoundTextures = new HashMap();
    public static final HashMap<String, Texture> subTextureMap = new HashMap();
    public static final HashMap<String, Texture> subTextureMap2 = new HashMap();
    public static final HashMap<String, TexturePackPage> texturePackPageMap = new HashMap();
    public static final HashMap<String, String> TexturePackPageNameMap = new HashMap();
    public final HashMap<String, Texture> subTextures = new HashMap();
    public Texture tex = null;
    static ByteBuffer SliceBuffer = null;
    static boolean bHasCache = false;
    static int percent = 0;
    public static int chl1 = 0;
    public static int chl2 = 0;
    public static int chl3 = 0;
    public static int chl4 = 0;
    static StringBuilder v = new StringBuilder(50);
    public static ArrayList<TexturePackPage.SubTextureInfo> TempSubTextureInfo = new ArrayList();
    public static ArrayList<String> tempFilenameCheck = new ArrayList();
    public static boolean bIgnoreWorldItemTextures = true;

    public static void LoadDir(String path) throws URISyntaxException {
    }

    public static void searchFolders(File fo) {
    }

    public static Texture getTexture(String _tex) {
        if (_tex.contains(".png")) {
            return Texture.getSharedTexture(_tex);
        } else {
            return subTextureMap.containsKey(_tex) ? (Texture)subTextureMap.get(_tex) : null;
        }
    }

    public static int readInt(InputStream in) throws EOFException, IOException {
        int int0 = in.read();
        int int1 = in.read();
        int int2 = in.read();
        int int3 = in.read();
        chl1 = int0;
        chl2 = int1;
        chl3 = int2;
        chl4 = int3;
        if ((int0 | int1 | int2 | int3) < 0) {
            throw new EOFException();
        } else {
            return (int0 << 0) + (int1 << 8) + (int2 << 16) + (int3 << 24);
        }
    }

    public static int readInt(ByteBuffer in) throws EOFException, IOException {
        byte byte0 = in.get();
        byte byte1 = in.get();
        byte byte2 = in.get();
        byte byte3 = in.get();
        chl1 = byte0;
        chl2 = byte1;
        chl3 = byte2;
        chl4 = byte3;
        return (byte0 << 0) + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
    }

    public static int readIntByte(InputStream in) throws EOFException, IOException {
        int int0 = chl2;
        int int1 = chl3;
        int int2 = chl4;
        int int3 = in.read();
        chl1 = int0;
        chl2 = int1;
        chl3 = int2;
        chl4 = int3;
        if ((int0 | int1 | int2 | int3) < 0) {
            throw new EOFException();
        } else {
            return (int0 << 0) + (int1 << 8) + (int2 << 16) + (int3 << 24);
        }
    }

    public static String ReadString(InputStream input) throws IOException {
        v.setLength(0);
        int int0 = readInt(input);

        for (int int1 = 0; int1 < int0; int1++) {
            v.append((char)input.read());
        }

        return v.toString();
    }

    public void loadFromPackFileDDS(BufferedInputStream input) throws IOException {
        String string0 = ReadString(input);
        tempFilenameCheck.add(string0);
        int int0 = readInt(input);
        boolean _boolean = readInt(input) != 0;
        TempSubTextureInfo.clear();

        for (int int1 = 0; int1 < int0; int1++) {
            String string1 = ReadString(input);
            int int2 = readInt(input);
            int int3 = readInt(input);
            int int4 = readInt(input);
            int int5 = readInt(input);
            int int6 = readInt(input);
            int int7 = readInt(input);
            int int8 = readInt(input);
            int int9 = readInt(input);
            if (string1.contains("ZombieWalk") && string1.contains("BobZ_")) {
                TempSubTextureInfo.add(new TexturePackPage.SubTextureInfo(int2, int3, int4, int5, int6, int7, int8, int9, string1));
            }
        }

        if (TempSubTextureInfo.isEmpty()) {
            int int10 = 0;

            do {
                int10 = readIntByte(input);
            } while (int10 != -559038737);
        } else {
            Texture texture0 = new Texture(string0, input, _boolean, Texture.PZFileformat.DDS);

            for (int int11 = 0; int11 < TempSubTextureInfo.size(); int11++) {
                TexturePackPage.SubTextureInfo subTextureInfo = (TexturePackPage.SubTextureInfo)TempSubTextureInfo.get(int11);
                Texture texture1 = texture0.split(subTextureInfo.x, subTextureInfo.y, subTextureInfo.w, subTextureInfo.h);
                texture1.copyMaskRegion(texture0, subTextureInfo.x, subTextureInfo.y, subTextureInfo.w, subTextureInfo.h);
                texture1.setName(subTextureInfo.name);
                this.subTextures.put(subTextureInfo.name, texture1);
                subTextureMap.put(subTextureInfo.name, texture1);
                texture1.offsetX = (float)subTextureInfo.ox;
                texture1.offsetY = (float)subTextureInfo.oy;
                texture1.widthOrig = subTextureInfo.fx;
                texture1.heightOrig = subTextureInfo.fy;
            }

            texture0.mask = null;
            texturePackPageMap.put(string0, this);
            int int12 = 0;

            do {
                int12 = readIntByte(input);
            } while (int12 != -559038737);
        }
    }

    public void loadFromPackFile(BufferedInputStream input) throws Exception {
        String string0 = ReadString(input);
        tempFilenameCheck.add(string0);
        int int0 = readInt(input);
        boolean boolean0 = readInt(input) != 0;
        if (boolean0) {
            boolean boolean1 = false;
        }

        TempSubTextureInfo.clear();

        for (int int1 = 0; int1 < int0; int1++) {
            String string1 = ReadString(input);
            int int2 = readInt(input);
            int int3 = readInt(input);
            int int4 = readInt(input);
            int int5 = readInt(input);
            int int6 = readInt(input);
            int int7 = readInt(input);
            int int8 = readInt(input);
            int int9 = readInt(input);
            if (!bIgnoreWorldItemTextures || !string1.startsWith("WItem_")) {
                TempSubTextureInfo.add(new TexturePackPage.SubTextureInfo(int2, int3, int4, int5, int6, int7, int8, int9, string1));
            }
        }

        Texture texture0 = new Texture(string0, input, boolean0);

        for (int int10 = 0; int10 < TempSubTextureInfo.size(); int10++) {
            TexturePackPage.SubTextureInfo subTextureInfo = (TexturePackPage.SubTextureInfo)TempSubTextureInfo.get(int10);
            Texture texture1 = texture0.split(subTextureInfo.x, subTextureInfo.y, subTextureInfo.w, subTextureInfo.h);
            texture1.copyMaskRegion(texture0, subTextureInfo.x, subTextureInfo.y, subTextureInfo.w, subTextureInfo.h);
            texture1.setName(subTextureInfo.name);
            this.subTextures.put(subTextureInfo.name, texture1);
            subTextureMap.put(subTextureInfo.name, texture1);
            texture1.offsetX = (float)subTextureInfo.ox;
            texture1.offsetY = (float)subTextureInfo.oy;
            texture1.widthOrig = subTextureInfo.fx;
            texture1.heightOrig = subTextureInfo.fy;
        }

        texture0.mask = null;
        texturePackPageMap.put(string0, this);
        int int11 = 0;

        do {
            int11 = readIntByte(input);
        } while (int11 != -559038737);
    }

    public static class SubTextureInfo {
        public int w;
        public int h;
        public int x;
        public int y;
        public int ox;
        public int oy;
        public int fx;
        public int fy;
        public String name;

        public SubTextureInfo(int _x, int _y, int _w, int _h, int _ox, int _oy, int _fx, int _fy, String _name) {
            this.x = _x;
            this.y = _y;
            this.w = _w;
            this.h = _h;
            this.ox = _ox;
            this.oy = _oy;
            this.fx = _fx;
            this.fy = _fy;
            this.name = _name;
        }
    }
}
